package com.fis.vaadin.utils;

import java.util.Vector;

import com.fis.mca.admin.AppClient;
import com.fis.mca.admin.AppUser;
import com.fis.vaadin.component.CommonButtonPanel;
import com.fis.vaadin.validator.PanelActionProvider;
import com.fss.ddtp.DDTP;
import com.vaadin.Application;
import com.vaadin.terminal.gwt.server.WebBrowser;

import eu.livotov.tpt.TPTApplication;

public class LogUtil {

	public static String getLogID(Application application, String actionType,
			String moduleName, CommonButtonPanel pnlAction) {
		// /////
		try {
			String strAction = actionType;
			switch (pnlAction.getAction()) {
			case PanelActionProvider.ACTION_ADD:
				strAction = "I";
				break;
			case PanelActionProvider.ACTION_EDIT:
				strAction = "U";
				break;
			case PanelActionProvider.ACTION_ADD_COPY:
				strAction = "I";
				break;
			case PanelActionProvider.ACTION_DELETE:
				strAction = "D";
				break;
			default:
				strAction = actionType;
			}
			DDTP request = new DDTP();
			request.setString("strModuleName", moduleName);
			request.setString("strUserName",
					((AppUser) application.getUser()).getUserName());
			request.setString("strActionType", strAction);
			if (TPTApplication.getCurrentApplication().getMainWindow()
					.getTerminal() instanceof WebBrowser) {
				WebBrowser browser = (WebBrowser) AppClient.getApplication()
						.getMainWindow().getTerminal();
				String strIpAddress = browser.getAddress();
				request.setString("strRemoteAdd", strIpAddress);
			}
			DDTP response = AppClient.sendRequest(
					"com.fss.admin.bean.ActionLogViewerBean", "onInsert",
					request);
			String strLogId = StringUtil.nvl(
					response.getString("out.strLogID"), "");
			return strLogId;
		} catch (Exception e) {
			e.printStackTrace();
			return "";
		}
	}

	//
	public static void logAfterInsert(String strLogID, String strTableName,
			String strCondition) {
		try {
			DDTP request = new DDTP();
			request.setString("in.strLogID", strLogID);
			request.setString("in.strTableName", strTableName);
			request.setString("in.strCondition", strCondition);
			AppClient.sendRequest("com.fss.admin.bean.ActionLogViewerBean",
					"logAfterInsert", request);
		} catch (Exception ex) {
			ex.printStackTrace();
		}
	}

	@SuppressWarnings("rawtypes")
	public static Vector logBeforeUpdate(String strLogID, String strTableName,
			String strCondition) {
		try {
			DDTP request = new DDTP();
			request.setString("in.strLogID", strLogID);
			request.setString("in.strTableName", strTableName);
			request.setString("in.strCondition", strCondition);
			DDTP response = AppClient.sendRequest(
					"com.fss.admin.bean.ActionLogViewerBean",
					"logBeforeUpdate", request);
			return (Vector) response.getReturn();

		} catch (Exception ex) {
			ex.printStackTrace();
			return null;
		}
	}

	@SuppressWarnings("rawtypes")
	public static void logAfterUpdate(Vector oldValue) {
		try {
			DDTP request = new DDTP();
			request.setVector("in.vtChangeID", oldValue);
			AppClient.sendRequest("com.fss.admin.bean.ActionLogViewerBean",
					"logAfterUpdate", request);
		} catch (Exception ex) {
			ex.printStackTrace();
		}
	}

	public static void logBeforeDelete(String strLogID, String strTableName,
			String strCondition) {
		try {
			DDTP request = new DDTP();
			request.setString("in.strLogID", strLogID);
			request.setString("in.strTableName", strTableName);
			request.setString("in.strCondition", strCondition);
			AppClient.sendRequest("com.fss.admin.bean.ActionLogViewerBean",
					"logBeforeDelete", request);
		} catch (Exception ex) {
			ex.printStackTrace();
		}
	}

	public static void logActionFailed(String strLogID) {
		try {
			DDTP request = new DDTP();
			request.setString("in.strLogID", strLogID);
			AppClient.sendRequest("com.fss.admin.bean.ActionLogViewerBean",
					"logActionFailed", request);
		} catch (Exception ex) {
			ex.printStackTrace();
		}
	}
}
